<template>
	<view class="login-container">
		<view class="login-top">
			<view class="logo-box">
				<image src="../../static/2301.jpg" mode=""></image>
			</view>
			<view class="motto">
				無限進步
			</view>
		</view>
		<view class="login-bottom">
			<view class="wechat-login" @click="wechatLogin">
				微信登录
			</view>
			<view class="other-login" @click="otherLogin">
				手机号登录/注册
			</view>
			<view class="other-login" @click="accountLogin">
				账号密码登录
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				code: '',
				appId: '',
			}
		},
		methods: {
			wechatLogin() {
				console.log('微信登录');
				uni.checkSession({
					provider: 'weixin',
					success: (res) => {
						console.log('checkSession 成功', res);
					},
					fail: (err) => {
						console.log('checkSession 失败', err);
					}
				})
				uni.login({
					provider: 'weixin',
					success: (res) => {
						this.code = res.code
						console.log('login 重新登录', {
							jscode: res.code,
							jscodeinfo: res
						});
					},
					fail: (err) => {
						console.log(err);
					}
				})
				const accountInfo = uni.getAccountInfoSync()
				console.log(accountInfo.miniProgram.appId);
				// this.appId = JSON.parse(JSON.stringify(accountInfo.miniProgram.appId))
				this.appId = accountInfo.miniProgram.appId
				uni.getUserProfile({
					desc: '获取信息授权',
					success: (res) => {
						console.log(res);
						this.login({
							name: res.userInfo.nickName,
							avatar: res.userInfo.avatarUrl,
							code: this.code,
							appId: this.appId
						})
					},
					fail: (err) => {
						console.log(err);
						uni.showToast({
							icon: 'error',
							title: '已拒绝授权，无法使用更多功能',
							duration: 1500
						})
					}
				})
			},
			otherLogin() {
				console.log('其他登录');
				uni.navigateTo({
					url: '/subcontract/register/register',
				})
			},
			accountLogin() {
				uni.navigateTo({
					url: '/subcontract/login/login',
				})
			},
			async login(user) {
				const { data: res } = await uni.$http.post('/public/users/wx/login', user)
			}
		}
	}
</script>

<style lang="scss">
.login-container {
	width: 100%;
	height: 100%;
	display: flex;
	padding-top: 50px;
	align-items: center;
	box-sizing: border-box;
	flex-direction: column;
	background-color: #fff;
	// justify-content: center;
	.logo-box {
		width: 100px;
		height: 100px;
		image {
			width: 100%;
			height: 100%;
		}
	}
	.motto {
		color: #fff;
		font-size: 24px;
		margin-top: 20px;
		font-weight: bold;
		text-align: center;
		letter-spacing: 2px;
		text-shadow: 0 0 10px #52e3fc, 0 0 20px aqua, 0 0 30px aquamarine;
	}
	.login-bottom {
		width: 90%;
		margin-top: 40px;
		.wechat-login {
			color: #fff;
			width: 100%;
			height: 45px;
			line-height: 45px;
			text-align: center;
			letter-spacing: 4px;
			background-color: #52e3fc;
			margin-bottom: 20px;
		}
		.other-login {
			width: 100%;
			height: 45px;
			color: #999;
			line-height: 45px;
			text-align: center;
			letter-spacing: 4px;
			margin-bottom: 20px;
			box-sizing: border-box;
			background-color: #fff;
			border: 1px solid #999;
		}
	}
}
</style>
